package com.igroupes.ovineherd.controller;

import com.igroupes.ovineherd.aop.LoginUser;
import com.igroupes.ovineherd.aop.SkipLogin;
import com.igroupes.ovineherd.service.FileService;
import com.igroupes.ovineherd.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@RequestMapping("file")
@LoginUser
public class FileController {


    @Autowired
    private FileService fileService;

    /**
     * 同时上传一个文件，返回url
     *
     * @param file
     * @return
     */
    @PostMapping("/upload")
    public ResultVO upload( MultipartFile file, HttpServletRequest request) throws IOException {
        return fileService.upload(file.getInputStream(), request);
    }

    @SkipLogin
    @GetMapping("/download/{key}")
    public void download(@PathVariable String key, HttpServletRequest request, HttpServletResponse response) throws IOException {
        fileService.download( key, request, response);
    }


}
